package cn.hrlly.tao2shou.view.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.listener.PauseOnScrollListener;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.hrlly.tao2shou.R;
import cn.hrlly.tao2shou.adapter.CommoditiesAdapter;
import cn.hrlly.tao2shou.domain.Commodities;
import cn.hrlly.tao2shou.domain.CommodityList;
import cn.hrlly.tao2shou.other.Config;
import cn.hrlly.tao2shou.presenter.CommoditiesListPresenter;
import cn.hrlly.tao2shou.presenter.CommoditiesListPresenterImpl;
import cn.hrlly.tao2shou.ui.RefreshLayout;
import cn.hrlly.tao2shou.utils.GpsUtil;
import cn.hrlly.tao2shou.utils.NetUtil;
import cn.hrlly.tao2shou.view.activity.CommodityDetailsActivity;
import cn.hrlly.tao2shou.view.view_interface.CommoditiesListView;

public class HomePageFragment extends Fragment implements CommoditiesListView{

    @Bind(R.id.lvCommodities)
    ListView lvCommodities;
    @Bind(R.id.refresh)
    RefreshLayout refreshLayout;
    private CommoditiesAdapter adapter;
    private GpsUtil gpsUtil;
    private ArrayList<Commodities> list;
    private static int page;
    private int pageCount;
    private int sortAccording;
    private CommoditiesListPresenter commoditiesListPresenter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView=inflater.inflate(R.layout.fragment_home_page, container, false);
        //绑定控件
        ButterKnife.bind(this,rootView);
        init();
        //判断是否有网，有网则连接后台显示，没网则缓存显示
        if(NetUtil.ping(getActivity())){
            commoditiesListPresenter.showCommoditiesList();
        }else {
            commoditiesListPresenter.showCacheCommoditiesList();
        }
        clickListview();
        refresh();
        return rootView;
    }

    //停止定位
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        gpsUtil.stopLocation();
    }

    @Override
    public void onPause() {
        super.onPause();
        gpsUtil.stopLocation();
    }

    @Override
    public void onStop() {
        super.onStop();
        gpsUtil.stopLocation();
    }

    @Override
    public void onStart() {
        super.onStart();
        gpsUtil.startLocation();
    }

    //初始化
    public void init(){
        gpsUtil=new GpsUtil(getActivity());
        gpsUtil.startLocation();
        list=null;
        page=1;
        pageCount=20;
        sortAccording=0;
        commoditiesListPresenter=new CommoditiesListPresenterImpl(this,getActivity());
    }

    //对listview的点击事件,跳转至详情页面
    public void clickListview(){
        lvCommodities.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent=new Intent(getActivity(),CommodityDetailsActivity.class);
                intent.putExtra(Config.key_release_id,list.get(position).getReleaseId());
                startActivity(intent);
            }
        });
    }

    //下拉刷新和加载更多
    public void refresh(){
        //下拉刷新
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refreshLayout.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        page=1;
                        list=null;
                        commoditiesListPresenter.showCommoditiesList();
                        refreshLayout.setRefreshing(false);
                    }
                },1000);
            }
        });

        //加载更多
        refreshLayout.setOnLoadListener(new RefreshLayout.OnLoadListener() {
            @Override
            public void onLoad() {
                refreshLayout.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        page++;
                        commoditiesListPresenter.showCommoditiesList();
                        refreshLayout.setLoading(false);
                    }
                },1000);
            }
        });
    }

    //以下是对fragment的一些操作
    @Override
    public void showListView(CommodityList commodityList) {
        if(list==null) {
            list = new ArrayList<Commodities>();
            list.addAll(commodityList.getCommodities());
            adapter=new CommoditiesAdapter(list,getActivity());
            lvCommodities.setAdapter(adapter);
        }else {
            //当下拉刷新或者更改sortAccording时page=1,list清空，刷新adapter,调用adapter.refresh
            //当加载更多时page++，list添加,刷新adapter,调用adapter.addMore
            list.addAll(commodityList.getCommodities());
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public int getPage() {
        return page;
    }

    @Override
    public int getPageCount() {
        return pageCount;
    }

    @Override
    public int getSortAccording() {
        return sortAccording;
    }

    @Override
    public double getLongitude() {
        return gpsUtil.returnLongitude();
    }

    @Override
    public double getLantitude() {
        return gpsUtil.returnLantitude();
    }

    @Override
    public void showMessage(String message) {
        Toast.makeText(getActivity(),message,Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showMessage(int message) {
        Toast.makeText(getActivity(),message,Toast.LENGTH_SHORT).show();
    }
}
